using System.Drawing;
using DevExpress.Utils;
using DevExpress.XtraBars;
using DevExpress.XtraBars.Ribbon;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraTab;
using E4RPDevExpress;
using E4RPTools;
using ZecaFil.Controls;

namespace ZecaFil
{
    public partial class MainRibbonForm : RibbonForm
    {
        private Controller _controller;

        #region Properties

        public Controller Controller
        {
            set
            {
                _controller = value;
                _controller.OwnerWindow = this;
                workOrdersXtraUserControl.Controller = _controller;
                salesXtraUserControl.Controller = _controller;
                contractTypesXtraUserControl.Controller = _controller;
                serviceTypesXtraUserControl.Controller = _controller;
                serviceDesignationXtraUserControl.Controller = _controller;
                userListXtraUserControl.Controller = _controller;                
                ControlsVisibility();
            }
        }

        #endregion

        private void SetupReportData()
        {
            relatorioDataSet.Merge(_controller.Reports());
            reportsRepositoryItemLookUpEdit.DataSource = relatorioDataSet.Relatorio;
            reportsRepositoryItemLookUpEdit.DisplayMember = relatorioDataSet.Relatorio.NomeColumn.ToString();
            reportsRepositoryItemLookUpEdit.ValueMember = relatorioDataSet.Relatorio.NomeColumn.ToString();
            reportsRepositoryItemLookUpEdit.Columns.Clear();
            reportsRepositoryItemLookUpEdit.Columns.AddRange(new[] {
                new LookUpColumnInfo("Nome", "Nome", 70, FormatType.None, "", true, HorzAlignment.Near)});
        }

        private bool ExistsVisibleTabPages()
        {
            foreach(XtraTabPage tabPage in xtraTabControl.TabPages)
                if(tabPage.PageVisible)
                    return true;
            return false;
        }

        private void ControlsVisibility()
        {
            workOrdersXtraTabPage.PageVisible = _controller.LoggedUserRunWorkOrders;
            wordOrderRibbonPageGroup.Visible = _controller.LoggedUserRunWorkOrders;
            salesXtraTabPage.PageVisible = _controller.LoggedUserRunSales;
            saleRibbonPageGroup.Visible = _controller.LoggedUserRunSales;
            if (_controller.LoggedUserProfile == (int)UserProfile.Administrator)
                return;
            userXtraTabPage.PageVisible = false;
            userRibbonPageGroup.Visible = false;
            contractTypesXtraTabPage.PageVisible = false;
            contractTypeRibbonPageGroup.Visible = false;
            serviceTypesXtraTabPage.PageVisible = false;
            serviceTypeRibbonPageGroup.Visible = false;
            serviceDesignationXtraTabPage.PageVisible = false;
            serviceDesignationRibbonPageGroup.Visible = false;

            gridRibbonPageGroup.Visible = ExistsVisibleTabPages();
            layoutRibbonPageGroup.Visible = ExistsVisibleTabPages();
        }


        public MainRibbonForm()
        {
            InitializeComponent();
            printSaleBarButtonItem.Enabled = false;            
            Load += MainRibbonFormLoad;
            xtraTabControl.SelectedPageChanged += XtraTabControlSelectedPageChanged;
            //
            newWorkOrderBarButtonItem.ItemClick += NewWorkOrderBarButtonItemItemClick;
            printWorkOrderBarButtonItem.ItemClick += PrintWorkOrderBarButtonItemItemClick;
            newSaleBarButtonItem.ItemClick += NewSaleBarButtonItemItemClick;
            printSaleBarButtonItem.ItemClick += PrintSaleBarButtonItemItemClick;
            newContractTypeBarButtonItem.ItemClick += NewContractTypeBarButtonItemItemClick;
            newServiceTypeBarButtonItem.ItemClick += NewServiceTypeBarButtonItemItemClick;
            newServiceDesignationBarButtonItem.ItemClick += NewServiceDesignationBarButtonItemItemClick;
            newUserBarButtonItem.ItemClick += NewUserBarButtonItemItemClick;
            //
            exportGridBarButtonItem.ItemClick += ExportGridBarButtonItemItemClick;
            columsGridBarButtonItem.ItemClick += ColumsGridBarButtonItemItemClick;
            printPreviewGridBarButtonItem.ItemClick += PrintPreviewGridBarButtonItemItemClick;
            printGridBarButtonItem.ItemClick += PrintBarGridButtonItemItemClick;
            //
            saveLayoutBarButtonItem.ItemClick += SaveLayoutBarButtonItemItemClick;
            loadLayoutBarButtonItem.ItemClick += LoadLayoutBarButtonItemItemClick;
            //
            aboutBarButtonItem.ItemClick += AboutBarButtonItemItemClick;
            exitBarButtonItem.ItemClick += ExitBarButtonItemItemClick;
            Closing += MainRibbonFormClosing;
            //
            reportsRepositoryItemLookUpEdit.EditValueChanging += ReportsRepositoryItemLookUpEditEditValueChanging;
            printPreviewReportBarButtonItem.ItemClick += PrintPreviewReportBarButtonItemItemClick;
            printReportBarButtonItem.ItemClick += PrintBarReportButtonItemItemClick;
            profitabilityBarButtonItem.ItemClick += ProfitabilityBarButtonItemItemClick;
            previewProfitabilityBarButtonItem.ItemClick += PreviewProfitabilityBarButtonItemItemClick;
            //
            ribbon.SelectedPage = ribbonPage;
        }

        void XtraTabControlSelectedPageChanged(object sender, TabPageChangedEventArgs e)
        {
            printWorkOrderBarButtonItem.Enabled = xtraTabControl.SelectedTabPage == workOrdersXtraTabPage;
            printSaleBarButtonItem.Enabled = xtraTabControl.SelectedTabPage == salesXtraTabPage;
        }

        void ReportsRepositoryItemLookUpEditEditValueChanging(object sender, ChangingEventArgs e)
        {
            printReportBarButtonItem.Enabled = e.NewValue != null;
            printPreviewReportBarButtonItem.Enabled = e.NewValue != null;
        }
       

        void MainRibbonFormLoad(object sender, System.EventArgs e)
        {
            printPreviewReportBarButtonItem.Enabled = false;
            printReportBarButtonItem.Enabled = false;            
            SetupReportData();
        }

        void NewWorkOrderBarButtonItemItemClick(object sender, ItemClickEventArgs e)
        {
            xtraTabControl.SelectedTabPage = workOrdersXtraTabPage;
            workOrdersXtraUserControl.AddDocument();            
        }

        void PrintWorkOrderBarButtonItemItemClick(object sender, ItemClickEventArgs e)
        {
             if(workOrdersXtraUserControl.GridView().GetSelectedRows().Length != 1)
                 return;
             workOrdersXtraUserControl.PrintDocument();
        }

        void NewSaleBarButtonItemItemClick(object sender, ItemClickEventArgs e)
        {
            xtraTabControl.SelectedTabPage = salesXtraTabPage;
            salesXtraUserControl.AddDocument();
        }

        void PrintSaleBarButtonItemItemClick(object sender, ItemClickEventArgs e)
        {
            if(salesXtraUserControl.GridView().GetSelectedRows().Length != 1)
                return;
            salesXtraUserControl.PrintDocument();
        }

        void NewContractTypeBarButtonItemItemClick(object sender, ItemClickEventArgs e)
        {
            xtraTabControl.SelectedTabPage = contractTypesXtraTabPage;
            contractTypesXtraUserControl.AddDocument();
        }

        void NewServiceTypeBarButtonItemItemClick(object sender, ItemClickEventArgs e)
        {
            xtraTabControl.SelectedTabPage = serviceTypesXtraTabPage;
            serviceTypesXtraUserControl.AddDocument();
        }

        void NewServiceDesignationBarButtonItemItemClick(object sender, ItemClickEventArgs e)
        {
            xtraTabControl.SelectedTabPage = serviceDesignationXtraTabPage;
            serviceDesignationXtraUserControl.AddDocument();
        }

        void NewUserBarButtonItemItemClick(object sender, ItemClickEventArgs e)
        {
            xtraTabControl.SelectedTabPage = userXtraTabPage;
            userListXtraUserControl.AddDocument();
        }

        void PrintPreviewReportBarButtonItemItemClick(object sender, ItemClickEventArgs e)
        {
            var reportName = reportBarEditItem.EditValue.ToString();
            var report = relatorioDataSet.Relatorio.FindByNome(reportName);
            if (report == null)
                return;
            _controller.Print(report.Ficheiro, true);
        }

        void PrintBarReportButtonItemItemClick(object sender, ItemClickEventArgs e)
        {
            var reportName = reportBarEditItem.EditValue.ToString();
            var report = relatorioDataSet.Relatorio.FindByNome(reportName);
            if(report == null)
                return;
            _controller.Print(report.Ficheiro, false);
        }
        
        void ExportGridBarButtonItemItemClick(object sender, ItemClickEventArgs e)
        {            
            var control = xtraTabControl.SelectedTabPage.Controls[0];
            DevExpressHelper.ExportToExcel(((IGridView)control).GridView());
        }

        void ColumsGridBarButtonItemItemClick(object sender, ItemClickEventArgs e)
        {
            var control = xtraTabControl.SelectedTabPage.Controls[0];
            var gridView = ((IGridView)control).GridView();
            DevExpressHelper.ColumnsCustomization(gridView);
        }

        void PrintPreviewGridBarButtonItemItemClick(object sender, ItemClickEventArgs e)
        {
            var control = xtraTabControl.SelectedTabPage.Controls[0];
            var gridView = ((IGridView)control).GridView();
            DevExpressHelper.ShowPrintPreview(gridView);
        }

        void PrintBarGridButtonItemItemClick(object sender, ItemClickEventArgs e)
        {
            var control = xtraTabControl.SelectedTabPage.Controls[0];
            var gridView = ((IGridView)control).GridView();
            gridView.GridControl.Print();
            DevExpressHelper.Print(gridView);
        }

        void SaveLayoutBarButtonItemItemClick(object sender, ItemClickEventArgs e)
        {
            var control = xtraTabControl.SelectedTabPage.Controls[0];
            var gridView = ((IGridView)control).GridView();
            var listName = ((IGridView)control).ListName;
            var layoutName = _controller.SaveLayout(this, listName, DevExpressHelper.SaveLayoutToXml(gridView));
            if (layoutName != "")
                layoutNameBarStaticItem.Caption = layoutName;
        }

        void LoadLayoutBarButtonItemItemClick(object sender, ItemClickEventArgs e)
        {
            var control = xtraTabControl.SelectedTabPage.Controls[0];
            var gridView = ((IGridView)control).GridView();
            var listName = ((IGridView)control).ListName;
            var layoutName = _controller.LoadLayout(gridView, listName);
            if (layoutName != "")
                layoutNameBarStaticItem.Caption = layoutName;
        }

        static void AboutBarButtonItemItemClick(object sender, ItemClickEventArgs e)
        {
            var product = new AboutLabelControl("Zecafil", new Font("Jokerman", 20F, FontStyle.Regular, GraphicsUnit.Point, 0), Color.ForestGreen, new Point(127, 38));
            var form = new AboutXtraForm { Version = "1.0.0.0", Product = product };
            form.ShowDialog();
        }

        void ExitBarButtonItemItemClick(object sender, ItemClickEventArgs e)
        {
            _controller.CloseConnections();
            Close();
        }

        void MainRibbonFormClosing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            _controller.CloseConnections();
        }

        void ProfitabilityBarButtonItemItemClick(object sender, ItemClickEventArgs e)
        {
            var profitabilityForm = new ProfitabilityXtraForm {Controller = _controller};
            if(profitabilityForm.ShowDialog() == System.Windows.Forms.DialogResult.Cancel)
                return;
            _controller.PrintProfitability("Rentabilidade.rpt", false, profitabilityForm.BeginDate, profitabilityForm.EndDate, profitabilityForm.Salesman);            
        }

        void PreviewProfitabilityBarButtonItemItemClick(object sender, ItemClickEventArgs e)
        {
            var profitabilityForm = new ProfitabilityXtraForm { Controller = _controller };
            if (profitabilityForm.ShowDialog() == System.Windows.Forms.DialogResult.Cancel)
                return;
            _controller.PrintPreviewProfitability("Rentabilidade.rpt", false, profitabilityForm.BeginDate, profitabilityForm.EndDate, profitabilityForm.Salesman);            
        }

    }
}